import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/layout/chenq' 
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import('../layout/layout.vue'),
    children: [{ //组长
        path: 'chenq',
        name: "chenq",
        meta: {
          icon:"el-icon-files",
          title: "教学分析",
          rules: ['admire', 'user']
        },
        component: () => import("../views/chenQ/ChenQ.vue")
      },
      { //黄梦典
        path: 'onepage',
        name: "onepage",
        meta: {
          icon:"el-icon-user",
          title: "用户分析",
          rules: ['admire', 'user']
        },
        component: () => import("../views/onepage/OnePage.vue")
      }, { //吴鑫
        path: 'wuxin',
        name: "wuxin",
        meta: {
          icon:"el-icon-receiving",
          title: "警员列表",
          rules: ['admire', 'user']
        },
        component: () => import("../views/wuxin/wuxin.vue")
      }, { //褚典
        path: 'chudian',
        meta: {
          icon:"el-icon-monitor",
          title: "人员管理",
          rules: ['admire', 'user']
        },
        name: "chudian",
        component: () => import("../views/chudian/chudian.vue")
      }, { //程梦婷
        path: 'chengmengting',
        name: "chengmengting",
        meta: {
          icon:"el-icon-notebook-1",
          title: "警员管理",
          rules: ['admire', 'user']
        },
        component: () => import("../views/chengmengting/chengmengting.vue")
      }, { //张颖
        path: 'zhangying',
        name: "zhangying",
        meta: {
          icon:"el-icon-turn-off",
          title: "警情管理",
          rules: ['admire', 'user']
        },
        component: () => import("../views/zhangying/zhangying.vue")
      },
      { //严昊宇
        path: 'yanhaoyu',
        name: "yanhaoyu",
        meta: {
          icon:"el-icon-edit",
          title: "停车记录",
          rules: ['admire', 'user']
        },
        component: () => import("../views/yanhaoyu/yanhaoyu.vue")
      },
      { //刘晶晶
        path: 'liujinjinone',
        name: "liujingjing",
        meta: {
          icon:"el-icon-office-building",
          title: "信息管理",
          rules: ['admire', 'user']
        },
        component: () => import("../views/liujingjing/liujinjinone.vue"),
        children: [{ //
          path: 'liujingjing',
          name: "liujingjing",
          meta: {
            icon:"el-icon-folder-opened",
            title: "学员管理",
            rules: ['admire', 'user']
          },
          component: () => import("../views/liujingjing/liujingjing.vue"),
        },
        { //
          path: 'liujjshuju',
          name: "liujjshuju",
          meta: {
            icon:"el-icon-document",
            title: "交通状况",
            rules: ['admire', 'user']
          },
          component: () => import("../views/liujingjing/liujjshuju.vue"),
        }
      ]
      },
      { //朱锐
        path: 'zhuruizon',
        name: "zhuruizon",
        meta: {
          icon:"el-icon-school",
          title: "杨永信诊所",
          rules: ['admire', 'user']
        },
        component: () => import("../views/zhurui/zhuruizon.vue"),
        children: [{ //朱锐
            path: 'zhurui',
            name: "zhurui",
            meta: {
              icon:"el-icon-first-aid-kit",
              title: "就诊名单",
              rules: ['admire', 'user']
            },
            component: () => import("../views/zhurui/zhurui.vue"),
          },
          { //朱锐
            path: 'zhuiruittn',
            name: "zhuiruittn",
            meta: {
              icon:"el-icon-notebook-2",
              title: "就诊视图",
              rules: ['admire', 'user']
            },
            component: () => import("../views/zhurui/zhuiruittn.vue"),
          }
        ]
      },

    ]
  },
  {
    path: "/login",
    name: "name",
    component: () => import("../views/Login.vue"),
  },
  {
    path: '*',
    component: () => import("../views/NotFound.vue")
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]
//设置含有权限的路由
const admireroutes = [{
  path: "gister",
  name: "name",
  meta: {
    icon:"el-icon-view",
    title: "数据视图",
    rules: ['admire']
  },
  component: () => import("../views/gister.vue"),
}, ]

const router = new VueRouter({
  routes
})
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

//设置路由权限
function getrouter(admireroutes, token) {
  var po = [];
  admireroutes.forEach(ele => {
    if (ele.meta.rules.includes(token)) {
      po.push(ele)
    }
  });
  return po
}
import store from "../store/index"
import {
  userdetail
} from "../api/interface.js"
import {
  getToken
} from "../untial/cook.js"
router.beforeEach((to, from, next) => {
  var path = to.path;
  var whitelist = ["/layout", "/layout/gister", "/layout/chenq", "/layout/onepage", 
  "/layout/wuxin", "/layout/chudian", "/layout/chengmengting", "/layout/zhangying", 
  "/layout/yanhaoyu", "/layout/liujinjinone", "/layout/zhuruizon", "/layout/zhuruizon/zhurui",
  "/layout/zhuruizon/zhuiruittn",, "/layout/liujinjinone/liujingjing", "/layout/liujinjinone/liujjshuju"];
  var token = getToken("token")
  if (token) {
    if (path == "/login") {
      next("/");
    } else {
      if (store.state.router.length == 0) {
        userdetail({
          token
        }).then(res => {
          if (res.code == 200) {
            store.commit("setuser", res.data);
            routes[1].children = routes[1].children.concat(getrouter(admireroutes, res.data.rule));
            store.commit('setrouter', routes);
            router.addRoutes(routes) //动态添加路由
            next({
              ...to,
              replace: true
            }) //解决addroutes动态添加路由的bug
          }

        })

      } else {
        next();
      }

    }
  } else {
    if (!whitelist.includes(path)) {
      next();
    } else {
      next("/login")
    }
  }
})
export default router